package com.hb;

import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;

import java.util.HashMap;

/**
 * 用于MyBatis查询库表中两列映射为键值对
 * @author jack
 */
public class MapResultHandler implements ResultHandler {

    private final HashMap mapResults = new HashMap<String,String>();

    private String key;

    private String value;

    @Override
    public void handleResult(ResultContext resultContext) {
        HashMap map = (HashMap)resultContext.getResultObject();
        if(map.get("key") != null) mapResults.put(map.get("key"), map.get("value"));
    }

    /**
     * 返回映射
     * @return 结果
     */
    public HashMap getMapResults() {
        return mapResults;
    }

    /**
     * @param key key
     * @param value value
     */
    public MapResultHandler(String key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 空构造
     */
    public MapResultHandler() {
    }

}
